package com.micro.school.h5.controller;

import com.alibaba.fastjson.JSON;
import com.micro.school.common.core.domain.R;
import com.micro.school.common.core.web.domain.AjaxResult;
import com.micro.school.system.api.RemoteOrderService;
import com.micro.school.system.api.domain.vo.order.PayStudentFeeItemVo;
import com.micro.school.system.api.domain.vo.order.PayStudentFeeVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/studentH5")
@Slf4j
public class BillManageController {

    @Resource
    private RemoteOrderService orderService;


    /**
     * H5 学生账单查询
     */
    @PostMapping("/getStudentBill")
    public AjaxResult getStudentBillH5(@RequestParam("studentId") Integer studentId,@RequestParam("feeYearId") Integer feeYearId) {
        R<List<PayStudentFeeVo>> studentFeePaymentH5 =  orderService.getStudentBillH5(studentId);
        if(null==studentFeePaymentH5 || null==studentFeePaymentH5.getData()){
            return AjaxResult.success(new ArrayList<>());
        }
        List<PayStudentFeeVo> feeVos = studentFeePaymentH5.getData();
        if(null!=feeVos){
            for(PayStudentFeeVo feeVo:feeVos){
                log.info("getStudentBillH5 feeVo.getFeeYearId()  {}",feeVo.getFeeYearId());
                log.info("getStudentBillH5 feeYearId  {}",feeYearId);
                if(null!=feeVo.getFeeYearId() && feeYearId.intValue()==feeVo.getFeeYearId().intValue()){
                    //处理掉 0 元的账单
                    filterZeroAmount(feeVo);
                    return AjaxResult.success(feeVo.getDetails());
                }
            }
        }
        return AjaxResult.success(new ArrayList<>());
    }
    private void filterZeroAmount(PayStudentFeeVo feeVo){
        List<PayStudentFeeItemVo> details = feeVo.getDetails();
        if(null==details || details.isEmpty()){
            return;
        }
        List<PayStudentFeeItemVo> result = new ArrayList<>();
        for(PayStudentFeeItemVo itemVo:details){
            if(null==itemVo.getTotalAmount()){
                continue;
            }
            if(itemVo.getTotalAmount()>0){
                result.add(itemVo);
            }
        }
        feeVo.setDetails(result);
    }
    /**
     * H5 学生支付查询
     */
    @PostMapping("/getStudentPay")
    public AjaxResult getStudentPay(@RequestParam("studentId") Integer studentId,@RequestParam("feeYearId") Integer feeYearId) {
        R<List<PayStudentFeeVo>> studentFeePaymentH5 =  orderService.getStudentPaymentInfo(studentId);
        log.info("studentFeePaymentH5 {}", JSON.toJSONString(studentFeePaymentH5));
        if(null==studentFeePaymentH5 || null==studentFeePaymentH5.getData()){
            return AjaxResult.success(new ArrayList<>());
        }
        List<PayStudentFeeVo> feeVos = studentFeePaymentH5.getData();
        if(null!=feeVos){
            for(PayStudentFeeVo feeVo:feeVos){
                log.info("getStudentPay feeVo.getFeeYearId()  {}",feeVo.getFeeYearId());
                log.info("getStudentPay feeYearId  {}",feeYearId);
                if(null!=feeVo.getFeeYearId() && feeYearId.intValue()==feeVo.getFeeYearId().intValue()){
                    return AjaxResult.success(feeVo.getPayDetails());
                }
            }
        }
        return AjaxResult.success(new ArrayList<>());
    }
}
